/**
 * @(#)OnlineSimulatorNumLogServiceImpl.java 2017年2月15日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IOnlineSimulatorNumLogDao;
import org.jsbd.boss.domian.OnlineSimulatorNumLog;
import org.jsbd.boss.domian.SeriesBean;
import org.jsbd.boss.service.IOnlineSimulatorNumLogService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 在线模拟器数量统计日志类
 * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
 * @version 1.0.0
 */
@Service("onlineSimulatorNumLogService")
public class OnlineSimulatorNumLogServiceImpl implements IOnlineSimulatorNumLogService {
	public static final Logger LOGGER = LoggerFactory.getLogger(OnlineSimulatorNumLogServiceImpl.class);

	@Autowired
	private IOnlineSimulatorNumLogDao onlineSimulatorNumLogDao;
	
	@Override
	public void save(OnlineSimulatorNumLog record) {
		record.setCreateTime(new Date());
		onlineSimulatorNumLogDao.save(record);
	}

	@Override
	public void saveList(List<OnlineSimulatorNumLog> record) {
		onlineSimulatorNumLogDao.saveList(record);
	}

	@Override
	public List<OnlineSimulatorNumLog> getOnlineSimulatorNumLogList(OnlineSimulatorNumLog record) {
		return onlineSimulatorNumLogDao.getOnlineSimulatorNumLogList(record);
	}
	
	@Override
	public Map<String,Object> getOnlineSimulatorNumLogForMap(OnlineSimulatorNumLog record) {
		condition(record);
		
		List<OnlineSimulatorNumLog> logList=onlineSimulatorNumLogDao.getOnlineSimulatorNumLogList(record);
		Map<String,Object> resultMap=new HashMap<String,Object>();
		
		List<String> categoriesList=new ArrayList<String>();
		List<SeriesBean> seriesList=new ArrayList<SeriesBean>();
		
		SeriesBean unBusy=new SeriesBean();
		unBusy.setName("空闲模拟器数");
		unBusy.setData(new ArrayList<Object>());
		SeriesBean total=new SeriesBean();
		total.setData(new ArrayList<Object>());
		total.setName("在线模拟器数");
		for(OnlineSimulatorNumLog log:logList){
			categoriesList.add(DateUtil.getCurrentDateHmsStr(log.getCreateTime()));
			unBusy.getData().add(log.getUnbusyNum());
			total.getData().add(log.getTotalNum());
		}
		
		seriesList.add(unBusy);
		seriesList.add(total);
		
		resultMap.put("categories", categoriesList);
		resultMap.put("series", seriesList);
		return resultMap;
	}
	
	
	
	private void condition(OnlineSimulatorNumLog record){
		
		if(StringUtils.isBlank(record.getStartTime())&&StringUtils.isBlank(record.getEndTime())){
			Calendar calender=Calendar.getInstance(); 
			calender.add(Calendar.MINUTE, -60);
			record.setStartDate(calender.getTime());
			return;
		}
		
		if(StringUtils.isNotBlank(record.getStartTime())){
			record.setStartDate(DateUtil.getCurrentYmdHmsDate(record.getStartTime()));
		}
		
		if(StringUtils.isNotBlank(record.getEndTime())){
			record.setEndDate(DateUtil.getCurrentYmdHmsDate(record.getEndTime()));
		}
		
		//只填写了endTime
        if(record.getStartDate()==null){
        	record.setCurrentDate(DateUtil.getCurrentDateStr(record.getEndDate()));
        	return;
		}
		
        //只填写了startTime
        if(record.getEndDate()==null){
        	record.setCurrentDate(DateUtil.getCurrentDateStr(record.getStartDate()));
        	return;
		}
		
        //2个都填写了，选择startTime
        record.setCurrentDate(DateUtil.getCurrentDateStr(record.getStartDate()));
		
	}
	
}
